﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;

namespace BOHVote.Common
{
    public static class common
    {
        public static string getAppSetting(string configurationKey)
        {
            return ConfigurationManager.AppSettings[configurationKey];
        }
        
        public static string RandomPassword()
        {
            string randomchars = "abcdefghijklmnopqrstuvwxyz|0123456789|ABCDEFGHIJKLMNOPQRSTUVWXYZ|";
            string tmpstr = "";
            int pwdlen = 6;
            int iRandNum;

            Random rnd = new Random();
            for (int i = 0; i < pwdlen; i++)
            {
                iRandNum = rnd.Next(randomchars.Length);
                tmpstr += randomchars[iRandNum];
            }
            return tmpstr;
        }

        /// <summary>
        /// 转换yyyy年MM月dd日为Date
        /// </summary>
        /// <param name="dateDate"></param>
        /// <returns></returns>
        public static DateTime changeDateFormatToDate(string dateDate)
        {
            //string strYear = dateDate.Split(new string[]{"年"} ,StringSplitOptions.None)[0];
            //string strMonth = dateDate.Split(new string[] { "年" }, StringSplitOptions.None)[1].Split(new string[] { "月" }, StringSplitOptions.None)[0];
            //string strDay = dateDate.Split(new string[] { "年" }, StringSplitOptions.None)[1].Split(new string[] { "月" }, StringSplitOptions.None)[1].Split(new string[] { "日" }, StringSplitOptions.None)[0];
            DateTime d = Convert.ToDateTime(dateDate);
            return d;
        }

        public static string ChangeDateFormatToString(DateTime dateDate)
        {
            string strYear = dateDate.Year.ToString();
            string strMonth = dateDate.Month.ToString();
            string strDay = dateDate.Day.ToString();
            return strYear + "-" + strMonth;// + "-" + strDay + "日";
        }

        public static string EducationName(string EducationLevl)
        {
            string returnvalue = string.Empty;
            switch (EducationLevl)
            {
                case "001":
                    returnvalue = "博士";
                    break;
                case "002":
                    returnvalue = "硕士";
                    break;
                case "003":
                    returnvalue = "本科";
                    break;
                case "004":
                    returnvalue = "专科";
                    break;
                case "005":
                    returnvalue = "普大";
                    break;
                case "006":
                    returnvalue = "硕士证书";
                    break;
                case "007":
                    returnvalue = "双学历";
                    break;
                case "008":
                    returnvalue = "硕（博）";
                    break;
                default:
                    break;
            }
            return returnvalue;
        }

        public static string ApplyTypeName(string applytype)
        {
            string returnvalue = string.Empty;
            switch (applytype)
            {
                case "B":
                    returnvalue = "破格";
                    break;
                case "N":
                    returnvalue = "正常";
                    break;
                case "U":
                    returnvalue = "转升";
                    break;
                case "C":
                    returnvalue = "转评";
                    break;
                case "E":
                    returnvalue = "优秀人才";
                    break;
                case "S":
                    returnvalue = "留学";
                    break;
                case "J":
                    returnvalue = "加评";
                    break;
                default:
                    break;
            }
            return returnvalue;
        }

        public static string GenderTypeName(string Gender)
        {
            string returnvalue = string.Empty;
            switch (Gender)
            {
                case "M":
                    returnvalue = "男";
                    break;
                case "F":
                    returnvalue = "女";
                    break;
                default:
                    break;
            }
            return returnvalue;
        }

        public static string ApplyStyle(string strStyle)
        {
            string returnValue = string.Empty;
            switch (strStyle)
            {
                case "G":
                    returnValue = "高级评委会";
                    break;
                case "S":
                    returnValue = "社区评委会";
                    break;
                default:
                    break;
            }
            return returnValue;
        }

        public static string GetResult(string result)
        {
            string returnvalue = string.Empty;
            switch (result)
            {
                case "A":
                    returnvalue = "援外";
                    break;
                case "F":
                    returnvalue = "免";
                    break;
                case "N":
                    returnvalue = "先评";
                    break;
                case "O":
                    returnvalue = "其他";
                    break;
                case "P":
                    returnvalue = "合格";
                    break;
                case "C":
                    returnvalue = "参评";
                    break;
                default:
                    break;
            }
            return returnvalue;
        }

        public static string Applyplight(string Applyplight)
        {
            string returnvalue = string.Empty;
            switch (Applyplight)
            {
                case "F":
                    returnvalue = "首次申报";
                    break;
                case "A":
                    returnvalue = "再次申报";
                    break;
                default:
                    break;
            }
            return returnvalue;
        }

        public static string GetFixJob(string result)
        {
            string returnvalue = string.Empty;
            switch (result)
            {
                case "F":
                    returnvalue = "免";
                    break;
                case "N":
                    returnvalue = "先评";
                    break;
                case "C":
                    returnvalue = "完成";
                    break;
                default:
                    break;
            }
            return returnvalue;
        }

        public static string UnitType(string type)
        {
            string returnvalue = string.Empty;
            switch (type)
            {
                case "Z":
                    returnvalue = "卫生中心";
                    break;
                case "G":
                    returnvalue = "主 管 局";
                    break;
                case "U":
                    returnvalue = "卫生单位";
                    break;
                case "P":
                    returnvalue = "个  人";
                    break;
                case "A":
                    returnvalue = "系统管理员";
                    break;
                default:
                    break;
            }
            return returnvalue;
        }

        public static string PersonalRank(string rank)
        {
            string returnvalue = string.Empty;
            switch (rank)
            {
                case "C":
                    returnvalue = "负责";
                    break;
                case "I":
                    returnvalue = "参与";
                    break;
                default:
                    break;
            }
            return returnvalue;
        }

        public static string AwardsRank(string rank)
        {
            string returnvalue = string.Empty;
            switch (rank)
            {
                case "0":
                    returnvalue = "特级";
                    break;
                case "1":
                    returnvalue = "一级";
                    break;
                case "2":
                    returnvalue = "二级";
                    break;
                case "3":
                    returnvalue = "二级";
                    break;
                default:
                    break;
            }
            return returnvalue;
        }

        public static string getLevel(string level)
        {
            string returnvalue = string.Empty;
            switch (level)
            {
                case "N":
                    returnvalue = "国家级";
                    break;
                case "P":
                    returnvalue = "省部级";
                    break;
                case "C":
                    returnvalue = "市  级";
                    break;
                case "A":
                    returnvalue = "区  级";
                    break;
                default:
                    break;
            }
            return returnvalue;
        }

        public static string getBirthdyFromIDCard(string IDCard)
        {
            string returnValue = string.Empty;
            if (!string.IsNullOrEmpty(IDCard))
            {
                if (IDCard.Trim().Length == 15)
                {
                    returnValue = "19" + IDCard.Trim().Substring(6, 2) + "-" + IDCard.Trim().Substring(8, 2);
                }
                else if (IDCard.Trim().Length == 18)
                {
                    returnValue = IDCard.Trim().Substring(6, 4) + "-" + IDCard.Trim().Substring(10, 2);
                }
            }
            return returnValue;
        }

        public static string getGenderByIDCard(string IDCard)
        {
            string returnValue = string.Empty;
            string Gender = string.Empty;
            if (!string.IsNullOrEmpty(IDCard))
            {
                if (IDCard.Trim().Length == 15)
                {
                    Gender = IDCard.Trim().Substring(12, 3);
                }
                else if (IDCard.Trim().Length == 18)
                {
                    Gender = IDCard.Trim().Substring(14, 3);
                }
                if (int.Parse(Gender) % 2 == 0)
                    returnValue = "F";
                else
                    returnValue = "M";
            }
            return returnValue;
        }

        public static string RemoveHTML(string Htmlstring)
        {
            //删除脚本   
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除HTML   
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);

            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);

            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "");
            Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

            return Htmlstring;
        }

        #region <<  文件操作  >>
        public static void CreateFolder(string strCreatePath)
        {
            if (strCreatePath.Trim().Length > 0)
            {
                try
                {
                    if (!Directory.Exists(strCreatePath))
                    {
                        Directory.CreateDirectory(strCreatePath);
                    }
                }
                catch
                {
                    throw;
                }
            }
        }

        #endregion

        public static void ToExcel(System.Web.UI.Control ctl)
        {
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            ctl.Page.EnableViewState = false;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }

        // take as input an array of strings and rearrange them in random order
        public static void shuffle(List<string> lista, Cmp Cmp)
        {   
            List<object> list = new List<object>();
            foreach (string item in lista)
                list.Add(item);
            list.Sort(Cmp);
            lista.Clear();
            foreach (string item in list)
                lista.Add(item);

        }  
    }

    public class Cmp : IComparer<object>
    {
        Random rnd = new Random();
        #region IComparer<object> 成员

        public int Compare(object x, object y)
        {
            if (x.Equals(y))
                return 0;
            return rnd.Next(3) - 1;
        }

        #endregion
    }

}
